package com.rico.kok.controller.post;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rico.kok.entity.Post;
import com.rico.kok.entity.User;
import com.rico.kok.entity.povos.PostUser;
import com.rico.kok.service.interf.PostService;
import com.rico.kok.service.interf.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：Rico_Skipper(JinYang Han)
 * @Date：2022/4/26 22:10
 */
@Controller
public class SearchController {

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @RequestMapping("/searchContent.htm")
    public String SearchContent(@RequestParam(value = "searchContent", defaultValue = "-1") String search,
                                @RequestParam(value = "type", defaultValue = "2") Integer type,
                                @RequestParam(value = "num", defaultValue = "1") Integer pageNum,
                                Model model,
                                HttpSession session) {
        //search传值为-1时，为翻页，设置上次搜索的内容
        if ("-1".equals(search)) {
            if (session.getAttribute("search") == null) {
                search = "";
            } else {
                search = session.getAttribute("search").toString();
            }
        }
        String url = "/searchContent.htm?type=" + type + "&num=";
        model.addAttribute("url", url);
        //查作者
        if (type == 1) {
            User user = new User();
            user.setUserName(search);
            PageHelper.startPage(pageNum, 6, "user_id desc");
            List<User> userList = userService.selectUsers(user);
            PageInfo<User> pageInfo=new PageInfo<>(userList);
            session.setAttribute("searchType", "作者");
            session.setAttribute("searchContent", "描述");
            model.addAttribute("pageInfo",pageInfo);
            return "search/searchAuthor";

        } else {
            Post post = new Post();
            if (type == 3) {
                post.setPostDetail(search);
                session.setAttribute("searchType", "描述");
            } else {
                post.setPostTitle(search);
                session.setAttribute("searchType", "标题");
            }
            session.setAttribute("searchContent", search);
            PageHelper.startPage(pageNum, 6, "post_id desc");
            ArrayList<PostUser> postUsers = postService.selectPosts(post);
            PageInfo<PostUser> pageInfo = new PageInfo(postUsers);
            model.addAttribute("pageInfo", pageInfo);
            return "search/searchContent";
        }
    }
}
